import { useEffect } from "react";
import { useReactive } from "ahooks";
import { notification } from "antd";
import dayjs from "dayjs";

const { ipcRenderer } = electron;

export default function globalModel() {
    let state = useReactive({
        isMaximized: true,
        currentWindow: remote.getCurrentWindow(),
        windowInnerHeight: window.innerHeight,
    });

    window.onresize = () => {
        state.windowInnerHeight = window.innerHeight;
    };

    useEffect(() => {
        const remoteSharedObject = remote.getGlobal("sharedObject");
        for (let key in remoteSharedObject) {
            state[key] = remoteSharedObject[key];
        }

        if (state.currentWindow) {
            state.currentWindow.on("maximize", () => {
                state.isMaximized = state.currentWindow.isMaximized();
            });

            state.currentWindow.on("unmaximize", () => {
                state.isMaximized = state.currentWindow.isMaximized();
            });
        }
    }, []);

    const UTCTimeToLocal = (time, format = "YYYY-MM-DD HH:mm:ss") => {
        if (!time) return "";
        let localTime = new Date().UTCTimeToLocal(time);
        if (format) return dayjs(localTime).format(format);
        return localTime;
    };

    const remoteData = async (options) => {
        options.organizationUuid = options.organizationUuid || window.organizationUuid;
        let response = await httpRequest(options);
        if (response.statusCode === 0) {
            return response.result ? response.result : true;
        } else if (response.statusCode === 1) {
            let msgs = [response.message];
            if (Array.isArray(response.result)) msgs = msgs.concat(response.result);
            if (typeof response.result === "string") msgs.push(response.result);
            notification.error({ message: "登录过期了", description: msgs.join("；") });
            return null;
        } else if (response.statusCode >= 400) {
            let msgs = [response.message];
            if (Array.isArray(response.result)) msgs = msgs.concat(response.result);
            if (typeof response.result === "string") msgs.push(response.result);
            notification.error({ message: "错误信息", description: msgs.join("；") });
            return null;
        } else {
            return null;
        }
    };

    const graphqlFetch = async (params) => {
        if (window.organizationUuid) {
            let serviceName = params.serviceName || "allinone";
            return await remoteData({
                object: "multi-organization-gateway",
                serviceName: `/graphql/${serviceName}_graphql_service`,
                organizationUuid: window.organizationUuid,
                params,
            });
        }
    };

    const openWindow = (winID, url, options = {}) => {
        let newOptions = JSON.parse(JSON.stringify(options));
        ipcRenderer.send("open-window", { winID, url, ...newOptions });
    };

    return { ...state, remoteData, UTCTimeToLocal, openWindow, graphqlFetch };
}
